package ar.edu.uba.fi.jmit.aspecter.parser;

import java.util.Map;

import ar.edu.uba.fi.jmit.aspecter.AspecterTestCase;

public class XMLMonitorEngineParserTestCase extends AspecterTestCase {

	private static String pathFileEnviromentDevelopment = "src/test/resources/testParser/MEDevelopment.jmit.xml";
	private static String pathFileEnviromentDevProd = "src/test/resources/testParser/MEDevelopmentProduction.jmit.xml";
	private static String pathFileNoEnviroment = "src/test/resources/testParser/MENoEnviroment.jmit.xml";
	private static String pathFileNoEnviromentMultiple = "src/test/resources/testParser/MENoEnviromentMultiple.jmit.xml";
//	private static String pathFileDefaultEnviroment = "src/test/resources/testParser/MEDefaultEnviroment.jmit.xml";
	
	public void testEnviromentDevelopment() {
		try {
			Map<String, String> props = XMLMonitorEngineParser.parse(pathFileEnviromentDevelopment, "development");
			assertEquals("http", props.get("protocol"));
			assertEquals("10.0.0.1", props.get("ip"));
			assertEquals("8080", props.get("port"));
			assertEquals("/monitorEngine", props.get("context"));
		} catch (Exception e) {
			fail(e);
		}
		try {
			Map<String, String> props = XMLMonitorEngineParser.parse(pathFileEnviromentDevelopment, "production");
			assertTrue(props.isEmpty());
		} catch (Exception e) {
			fail(e);
		}
		try {
			Map<String, String> props = XMLMonitorEngineParser.parse(pathFileEnviromentDevelopment, "testing");
			assertTrue(props.isEmpty());
		} catch (Exception e) {
			fail(e);
		}
		try {
			Map<String, String> props = XMLMonitorEngineParser.parse(pathFileEnviromentDevelopment, "bla bla");
			assertTrue(props.isEmpty());
		} catch (Exception e) {
			fail(e);
		}
	}
	
	public void testEnviromentDevProd() {
		try {
			Map<String, String> props = XMLMonitorEngineParser.parse(pathFileEnviromentDevProd, "development");
			assertEquals("http", props.get("protocol"));
			assertEquals("10.0.0.1", props.get("ip"));
			assertEquals("8080", props.get("port"));
			assertEquals("/monitorEngine", props.get("context"));
		} catch (Exception e) {
			fail(e);
		}
		try {
			Map<String, String> props = XMLMonitorEngineParser.parse(pathFileEnviromentDevProd, "production");
			assertEquals("http", props.get("protocol"));
			assertEquals("10.10.0.10", props.get("ip"));
			assertEquals("8880", props.get("port"));
			assertEquals("/monitorEngineProduction", props.get("context"));
		} catch (Exception e) {
			fail(e);
		}
		try {
			Map<String, String> props = XMLMonitorEngineParser.parse(pathFileEnviromentDevProd, "testing");
			assertTrue(props.isEmpty());
		} catch (Exception e) {
			fail(e);
		}
		try {
			Map<String, String> props = XMLMonitorEngineParser.parse(pathFileEnviromentDevProd, "bla bla");
			assertTrue(props.isEmpty());
		} catch (Exception e) {
			fail(e);
		}
	}
	
	public void testNoEnviroment() {
		try {
			Map<String, String> props = XMLMonitorEngineParser.parse(pathFileNoEnviroment, "development");
			assertEquals("http", props.get("protocol"));
			assertEquals("10.0.0.1", props.get("ip"));
			assertEquals("8080", props.get("port"));
			assertEquals("/monitorEngine", props.get("context"));
		} catch (Exception e) {
			fail(e);
		}
		try {
			Map<String, String> props = XMLMonitorEngineParser.parse(pathFileNoEnviroment, "production");
			assertEquals("http", props.get("protocol"));
			assertEquals("10.0.0.1", props.get("ip"));
			assertEquals("8080", props.get("port"));
			assertEquals("/monitorEngine", props.get("context"));
		} catch (Exception e) {
			fail(e);
		}
		try {
			Map<String, String> props = XMLMonitorEngineParser.parse(pathFileNoEnviroment, "testing");
			assertEquals("http", props.get("protocol"));
			assertEquals("10.0.0.1", props.get("ip"));
			assertEquals("8080", props.get("port"));
			assertEquals("/monitorEngine", props.get("context"));
		} catch (Exception e) {
			fail(e);
		}
		try {
			Map<String, String> props = XMLMonitorEngineParser.parse(pathFileNoEnviroment, "bla bla");
			assertEquals("http", props.get("protocol"));
			assertEquals("10.0.0.1", props.get("ip"));
			assertEquals("8080", props.get("port"));
			assertEquals("/monitorEngine", props.get("context"));
		} catch (Exception e) {
			fail(e);
		}
	}
	
	public void testNoEnviromentMultiple() {
		try {
			XMLMonitorEngineParser.parse(pathFileNoEnviromentMultiple, "development");
			fail("Exception should be thrown");
		} catch (Exception e) {
			//expected
		}
		try {
			XMLMonitorEngineParser.parse(pathFileNoEnviromentMultiple, "production");
			fail("Exception should be thrown");
		} catch (Exception e) {
			//expected
		}
		try {
			XMLMonitorEngineParser.parse(pathFileNoEnviromentMultiple, "testing");
			fail("Exception should be thrown");
		} catch (Exception e) {
			//expected
		}
		try {
			XMLMonitorEngineParser.parse(pathFileNoEnviromentMultiple, "bla bla");
			fail("Exception should be thrown");
		} catch (Exception e) {
			//expected
		}
	}
	
	/*
	 * Esto no se va a implementar en esta etapa.
	 */
//	public void testDefaultEnviroment() {
//		try {
//			Map<String, String> props = XMLMonitorEngineParser.parse(pathFileDefaultEnviroment, "development");
//			assertEquals("http", props.get("protocol"));
//			assertEquals("10.0.0.1", props.get("ip"));
//			assertEquals("8080", props.get("port"));
//			assertEquals("/monitorEngine/", props.get("context"));
//		} catch (Exception e) {
//			fail(e);
//		}
//		try {
//			Map<String, String> props = XMLMonitorEngineParser.parse(pathFileDefaultEnviroment, "production");
//			assertEquals("http", props.get("protocol"));
//			assertEquals("192.198.0.1", props.get("ip"));
//			assertEquals("8888", props.get("port"));
//			assertEquals("/monitorEngineProduction/", props.get("context"));
//		} catch (Exception e) {
//			fail(e);
//		}
//		try {
//			Map<String, String> props = XMLMonitorEngineParser.parse(pathFileDefaultEnviroment, "testing");
//			assertEquals("http", props.get("protocol"));
//			assertEquals("10.0.0.1", props.get("ip"));
//			assertEquals("8080", props.get("port"));
//			assertEquals("/monitorEngine/", props.get("context"));
//		} catch (Exception e) {
//			fail(e);
//		}
//		try {
//			Map<String, String> props = XMLMonitorEngineParser.parse(pathFileDefaultEnviroment, "bla bla");
//			assertEquals("http", props.get("protocol"));
//			assertEquals("10.0.0.1", props.get("ip"));
//			assertEquals("8080", props.get("port"));
//			assertEquals("/monitorEngine/", props.get("context"));
//		} catch (Exception e) {
//			fail(e);
//		}
//	}
}
